package com.lottery.service.pc28.accoutService.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lottery.dao.pc28.accoutDao.ServerOnlineDao;
import com.lottery.dao.pc28.accoutDao.UserDao;
import com.lottery.entity.User;
import com.lottery.service.pc28.accoutService.ServerOnlineService;
import com.lottery.utils.CustomerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ServerOnlineServiceimpl implements ServerOnlineService {

	@Autowired
	private ServerOnlineDao serveronlineDao;

	@Autowired
	private UserDao userdao;

	// @Autowired
	// private JedisClient jedisClient;
	// private CustomerContext customerContext = CustomerContext.getInstance();

	// 日志
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * int clientId 客户id // 获取客服id
	 */
	@Override
	public String getCustomerId(int clientId) {
		JSONObject jObject = new JSONObject();
		try {
			// 日志
			logger.info("clientId:{}", clientId);
			// 获取 客服 id
			CustomerContext customerContext = CustomerContext.getInstance();

			logger.debug("customer hashcode:{}", customerContext.hashCode());
			int serverId = customerContext.allocateServer(clientId);

			if (serverId == -1) {
				jObject.put("result", "1");
				jObject.put("msg", "当前没有客服服务 ");
				return jObject.toJSONString();
			} else if (serverId == -2) {
				jObject.put("result", "2");
				jObject.put("msg", String.format("前面还有%d人排队，请稍等", customerContext.getserviceRequestListSize() - 1));
				return jObject.toJSONString();
			}

			jObject.put("result", "0");
			jObject.put("serverId", serverId);
		} catch (Exception e) {
			e.printStackTrace();
			jObject.put("result", "1");
			jObject.put("msg", "获取失败");
		}
		return jObject.toJSONString();
	}

	// 客服完成
	/*
	 * 客服id int userId ;客户id int clientId
	 */
	@Override
	public String finishService(int serverId, int clientId) {
		JSONObject jObject = new JSONObject();
		try {
			// 当 客服和客户完成 交互后
			// serveronlineDao.deleteServerOnline(clientId);
			CustomerContext customerContext = CustomerContext.getInstance();
			customerContext.finishService(serverId, clientId);
			// 返回状态
			jObject.put("result", "0");
			jObject.put("msg", "成功完成");

		} catch (Exception e) {
			e.printStackTrace();
			// 返回状态
			jObject.put("result", "1");
			jObject.put("msg", "完成失败");
		}

		// 日志
		logger.info("jObject:{}", jObject);
		return jObject.toJSONString();
	}

	// 客服下线
	/*
	 * 客服id int userId
	 */
	@Override
	public String serverOffline(int serverId) {
		JSONObject jObject = new JSONObject();
		try {
			// 当客服关闭客服系统时，判定下线（或一些不可控制的其他因素 ）
			// user_id是客服 id
			CustomerContext customerContext = CustomerContext.getInstance();
			customerContext.serverOffline(serverId);
			// 返回状态
			jObject.put("result", "0");
			jObject.put("msg", "客服已下线");

		} catch (Exception e) {
			jObject.put("result", "1");
			jObject.put("msg", "失败 ");
		}
		// 日志
		logger.info("jObject:{}", jObject);
		return jObject.toJSONString();
	}

	// 客户 完成咨询
	/*
	 * 客服id int userId
	 */
	@Override
	public String userFinishServer(int userId) {
		JSONObject jObject = new JSONObject();
		try {
			// 当客服关闭客服系统时，判定下线（或一些不可控制的其他因素 ）
			// user_id是客服 id
			CustomerContext customerContext = CustomerContext.getInstance();
			customerContext.userFinishServer(userId);
			// 返回状态
			jObject.put("result", "0");
			jObject.put("msg", "客户完成咨询 ");

		} catch (Exception e) {
			jObject.put("result", "1");
			jObject.put("msg", "失败 ");
		}
		// 日志
		logger.info("jObject:{}", jObject);
		return jObject.toJSONString();
	}

	@Override
	public String setServerIdle(int serverId, byte idle) {

		JSONObject jObject = new JSONObject();
		try {
			CustomerContext customerContext = CustomerContext.getInstance();
			if (customerContext.serverIdle(serverId, idle)) {
				jObject.put("result", "0");
				jObject.put("msg", "设置成功");
				return jObject.toJSONString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		jObject.put("result", "1");
		jObject.put("msg", "设置失败 ");
		return jObject.toJSONString();
	}

	@Override
	public String getWaitClientInfo(int serverId) {

		Map<String, Object> maxmap = new HashMap<String, Object>();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			CustomerContext customerContext = CustomerContext.getInstance();
			List<Integer> ids = customerContext.getserviceRequestList();
			if (!ids.isEmpty()) {
				List<User> users = userdao.selectByUserId(ids);
				for (User one : users) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("userId", one.getUserId());
					map.put("userName", one.getUserName());
					map.put("headPic", one.getHeadPic());
					list.add(map);
				}
			}
			maxmap.put("array", list);
			maxmap.put("result", "0");
		} catch (Exception e) {
			e.printStackTrace();
			maxmap.put("result", "1");
			maxmap.put("msg", "失败");
		}
		// 转成JsontoString
		String json = JSON.toJSONString(maxmap, true);
		// 日志
		logger.info("getWaitClientInfo out json:{}", json);
		return json;
	}

}
